import type { OrderCreateParams, OrderListParams, OrderListResult, OrderLogisticResult, OrderPreResult, OrderResult } from "@/types/order"
import { deleteAction, getAction, postAction, putAction } from "@/utils/http"

// 获取订单页信息
export const getOrderPreApi=()=>{
    return getAction<OrderPreResult>({
        url: '/member/order/pre'
    })
}

// 获取立即购买订单页信息
export const getOrderPreNowApi=(data: {
    skuId: string,
    count: string,
    addressId?: string
})=>{
    return getAction<OrderPreResult>({
        url: '/member/order/pre/now',
        data
    })
}

// 提交订单接口
export const postOrderApi = (data: OrderCreateParams)=>{
    return postAction<{id: string}>({
        url: '/member/order',
        data
    })
}

// 获取订单详情接口
export const getOrderDetailApi = (id: string)=>{
    return getAction<OrderResult>({
        url: `/member/order/${id}`
    })
}

/**
 * 填写订单-再次购买
 * @param id 订单id
 */
export const getMemberOrderRepurchaseByIdAPI = (id: string) => {
    return getAction<OrderPreResult>({
      url: `/member/order/repurchase/${id}`
    })
  }

/**
 * 获取微信支付参数
 * @param data orderId 订单id
 */
export const getPayWxPayMiniPayAPI = (data: { orderId: string }) => {
  return getAction<WechatMiniprogram.RequestPaymentOption>({
    url: '/pay/wxPay/miniPay',
    data,
  })
}

/**
 * 模拟支付-内测版
 * @param data orderId 订单id
 */
export const getPayMockAPI = (data: { orderId: string }) => {
  return getAction({
    url: '/pay/mock',
    data,
  })
}

/**
 * 模拟发货-内测版
 * @description 在DEV环境下使用，仅在订单状态为待发货时，可模拟发货，调用后订单状态修改为待收货，包含模拟物流。
 * @param id 订单id
 */
export const getMemberOrderConsignmentByIdAPI = (id: string) => {
    return getAction({
      url: `/member/order/consignment/${id}`,
    })
  }

  /**
 * 确认收货
 * @description 仅在订单状态为待收货时，可确认收货。
 * @param id 订单id
 */
export const putMemberOrderReceiptByIdAPI = (id: string) => {
    return putAction<OrderResult>({
      url: `/member/order/${id}/receipt`,
    })
  }

  /**
 * 获取订单物流
 * @description 仅在订单状态为待收货，待评价，已完成时，可获取物流信息。
 * @param id 订单id
 */
export const getMemberOrderLogisticsByIdAPI = (id: string) => {
    return getAction<OrderLogisticResult>({
      url: `/member/order/${id}/logistics`,
    })
  }

  /**
 * 删除订单
 * @description 仅在订单状态为待评价，已完成，已取消时，可删除订单。
 * @param data ids 订单集合
 */
export const deleteMemberOrderAPI = (data: { ids: string[] }) => {
    return deleteAction({
      url: `/member/order`,
      data,
    })
  }

  /**
 * 取消订单
 * @description 仅在订单状态为待付款时，可取消订单。
 * @param id 订单id
 * @param data cancelReason 取消理由
 */
export const getMemberOrderCancelByIdAPI = (id: string, data: { cancelReason: string }) => {
    return putAction<OrderResult>({
      url: `/member/order/${id}/cancel`,
      data,
    })
  }

  // 获取订单列表
  export const getOrderListAPI = (data: OrderListParams) => {
    return getAction<OrderListResult>({
      url: `/member/order`,
      data,
    })
  }